package com.example.demo.security;

import com.example.demo.DemoApplication;
import com.example.demo.model.User;
import com.example.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserRepository userRepository;

    /**
     * 登录逻辑实现
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//
//        if(username=="")
//        {
//            System.out.println("username is null");
//        }
        System.out.println("loadUserByUsername: "+username);
        User user=userRepository.findUserByUsername(username);
        if(user == null)
        {
            System.out.println("no user");
            throw new UsernameNotFoundException("没有该用户");
        }
        System.out.println("user:"+user.getUsername());
        return new UserDetailsImpl(user);
    }
}
